/************************************************************************/
/*Core                                                                  */
/************************************************************************/
#include "Core/BaseEntity.h"

//Cross for Rectangle shape destruction
#include "Core/RectangleShape.h"

namespace TD_Core {
	/************************************************************************/
	/* Private functions                                                   */
	/************************************************************************/
	bool BaseEntity::initValues() {
		//Default Coord2D initializes the class at the origin (x = y = 0);
		/*m_pImage = shared_ptr<SDLImage>();*/
		return true;
	}

	/************************************************************************/
	/* Protected functions                                                  */
	/************************************************************************/
	void BaseEntity::blitSurface(SDL_Surface * const g_destSurface, SDL_Rect *g_pClippingRect /* = 0 */) const {
		SDL_Rect worldOffset;
		worldOffset.x = m_position.x;
		worldOffset.y = m_position.y;
		
		/*m_pImage->blitSurface(g_destSurface, &worldOffset, g_pClippingRect);*/
	}
	void BaseEntity::blitSurface(SDL_Surface *g_srcSurface, SDL_Surface *const g_destSurface, 
		const Coord2D &g_position, SDL_Rect *g_pClippingRect /* = 0 */) const {
		
		SDL_Rect worldOffset;
		worldOffset.x = m_position.x;
		worldOffset.y = m_position.y;

		SDL_BlitSurface(g_srcSurface, g_pClippingRect, g_destSurface, &worldOffset);

	}

	/************************************************************************/
	/* Constructors                                                         */
	/************************************************************************/
	BaseEntity::BaseEntity() {
		initValues();
	}

	BaseEntity::BaseEntity(const BaseEntity &rhs) {
		/*m_pImage = rhs.getImage();*/
		m_shapes = rhs.getShapesVector();
	}

	BaseEntity & BaseEntity::operator=(const BaseEntity &rhs) {
	/*	m_pImage = rhs.getImage();*/
		m_shapes = rhs.getShapesVector();
		return(*this);
	}

	BaseEntity::BaseEntity(Coord2D g_position, float g_rotationAngle /* = 0 */) 
		: m_position(g_position), m_rotationAngle(g_rotationAngle)	
	{
		initValues();
		m_position = g_position;
		m_rotationAngle = g_rotationAngle;
	}

	BaseEntity::~BaseEntity() {
		//As the shared pointer of the image looses it's scope
		// it will be destructed when needed.
		vector<BaseShape *>::iterator iter;
		for(iter = m_shapes.begin(); iter != m_shapes.end(); ) {
			if(dynamic_cast<RectangleShape *>(*iter)) {
				Utils::safeDelete<RectangleShape>(dynamic_cast<RectangleShape *>(*iter));
			} else {	
				delete *iter;
			}
			iter = m_shapes.erase(iter);
		}
	}

}
